package com.soft.javase.day10.practice.collectors;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author yzh
 * @desc TODO
 * @date 2025/5/4
 */
public class CollectorsDemo {
    public static void main(String[] args) {

        String[] strings = {"apple", "banana", "cherry"};
        Integer[] numbers = {1, 2, 3, 4, 5};

        System.out.println("将英文字符串的数组中的元素全部变为大写，以集合输出");
        List<String> upperCaseList = Arrays.stream(strings)
                .map(String::toUpperCase)
                .collect(Collectors.toList());
        System.out.println(upperCaseList);

        System.out.println("\n将整数的数组中的元素全部加3，以集合输出");
        List<Integer> plus3List = Arrays.stream(numbers)
                .map(n -> n + 3)
                .collect(Collectors.toList());
        System.out.println(plus3List);

        System.out.println("\n统计员工人数、平均工资、工资总额、最高工资、统计工资的所有信息");
        List<Employee> employees = Arrays.asList(
                new Employee("张三", 28, 7500.0),
                new Employee("李四", 36, 8500.0),
                new Employee("赵六", 35, 8500.0),
                new Employee("张明", 36, 9500.0),
                new Employee("王五", 38, 9500.0)
        );

        long count = employees.stream().collect(Collectors.counting());
        double avgSalary = employees.stream().collect(Collectors.averagingDouble(Employee::getSalary));
        double totalSalary = employees.stream().collect(Collectors.summingDouble(Employee::getSalary));
        Optional<Employee> maxSalaryEmp = employees.stream().collect(Collectors.maxBy(Comparator.comparingDouble(Employee::getSalary)));
        DoubleSummaryStatistics salaryStats = employees.stream().collect(Collectors.summarizingDouble(Employee::getSalary));

        System.out.println("员工人数: " + count);
        System.out.println("平均工资: " + avgSalary);
        System.out.println("工资总额: " + totalSalary);
        System.out.println("最高工资: " + maxSalaryEmp.map(Employee::getSalary).orElse(0.0));
        System.out.println("完整工资统计: " + salaryStats);

        System.out.println("\n将员工按薪资是否高于8000分为两部分");
        Map<Boolean, List<Employee>> partition = employees.stream()
                .collect(Collectors.partitioningBy(e -> e.getSalary() > 8000.0));
        System.out.println("薪资≤8000: " + partition.get(false));
        System.out.println("薪资>8000: " + partition.get(true));

        System.out.println("\n将所有员工的名称用逗号拼接");
        String join = employees.stream()
                .map(Employee::getName)
                .collect(Collectors.joining(","));
        System.out.println(join);

    }
}
